﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.SM
{
    /// <summary>
    /// Index 的摘要说明。
    /// </summary>
    public class Index : Page
    {
        protected Button btnDelete;
        protected Button btnRead;
        protected DataGrid dgMsgList;

        protected int DispType
        {
            get;
            private set;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            string Username = (Request.QueryString["Username"] != null) ? Request.QueryString["Username"] : SiteContext.Current.UserName;
            DispType = QSUtil.GetPositiveInt32("DispType", 1);

            if (!Page.IsPostBack)
            {
                BindGrid(Username, DispType);
                btnDelete.Attributes["onclick"] = "javascript:return confirm('您确认要删除吗?');";
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<int> list = new List<int>();
            foreach (DataGridItem dgi in dgMsgList.Items)
            {
                var cb = (CheckBox)(dgi.Cells[0].Controls[1]);
                if (cb.Checked)
                {
                    int i = dgi.ItemIndex;
                    string id = dgMsgList.DataKeys[i].ToString();

                    list.Add(int.Parse(id));
                }
            }

            // 选择为空
            if (list.Count == 0)
            {
                SiteContext.Current.AlertAndRedirect("请选择消息!", "Index.aspx?DispType=" + DispType);
            }
            else if (SMS.MsgDelete(list.ToArray()))
            {
                SiteContext.Current.AlertAndRedirect("短讯删除成功", "Index.aspx?DispType=" + DispType);
            }
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            string Username = (Request.QueryString["Username"] != null) ? Request.QueryString["Username"] : SiteContext.Current.UserName;
            string ids = "";
            foreach (DataGridItem dgi in dgMsgList.Items)
            {
                var cb = (CheckBox)(dgi.Cells[0].Controls[1]);
                if (cb.Checked)
                {
                    int i = dgi.ItemIndex;
                    string id = dgMsgList.DataKeys[i].ToString();
                    ids += id + ",";
                }
            }
            if (ids.EndsWith(","))
            {
                ids = ids.Substring(0, ids.Length - 1);
            }
            //选择为空
            if (ids == String.Empty)
            {
                Response.Write("<script language=javascript>alert('请选择消息!');window.location='Index.aspx?DispType=" + Session["MsgDispType"] + "';</script>");
            }
            else
            {
                if (SMS.ReadMsg(ids, Username))
                {
                    Response.Write("<script language=javascript>alert('短讯已阅!');window.location='Index.aspx?DispType=" + Session["MsgDispType"] + "';</script>");
                }
            }
        }

        #region 数据绑定至DataGrid

        /// <summary>
        /// 将用户的短讯记录显示在datagrid上
        /// </summary>
        protected void BindGrid(string Username, int dispType)
        {
            var sm = new SMS();
            if (dispType == 1) //我的所的接收
            {
                try
                {
                    using (var dr = sm.GetMyReceive(Username))
                    {
                        dgMsgList.DataSource = Tools.ConvertDataReaderToDataTable(dr).DefaultView;
                    }
                    dgMsgList.DataBind();
                }
                catch
                {
                    Server.Transfer("../Error.aspx");
                }
            }

            if (dispType == 2) //我所发送
            {
                try
                {
                    using (var dr = sm.GetMySent(Username))
                    {
                        dgMsgList.DataSource = Tools.ConvertDataReaderToDataTable(dr).DefaultView;
                    }
                    dgMsgList.DataBind();
                }
                catch
                {
                    Server.Transfer("../Error.aspx");
                }
            }

            switch (dispType)
            {
                case 1:
                    dgMsgList.Columns[2].Visible = false; //　隐藏收件人
                    break;
                case 2:
                    dgMsgList.Columns[1].Visible = false; //　隐藏收件人
                    btnRead.Visible = false; //隐藏已阅按钮
                    btnDelete.Visible = false; //隐藏删除按钮
                    break;
                default:
                    break;
            }
        }

        #endregion

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dgMsgList.CurrentPageIndex = e.NewPageIndex;
            BindGrid(SiteContext.Current.UserName, DispType);
        }

        #endregion

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.btnRead.Click += new System.EventHandler(this.btnRead_Click);
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            this.ID = "Index";
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion
    }
}